const express = require("express");
const exApp = require("../index");
const router = express.Router();
const mysql = require("../connect/pomsql");
function Fun() {
	var date = new Date();
	date = date.toISOString().slice(0, 19).replace("T", " ");
	date = date.split(" ");
	date[1] = date[1].split(":")
	date[1][0] = date[1][0]*1+8+''
	date[1] = date[1].join(':')
	date = date.join(" ")
	return date
}
router.post("/adduser", async function (req, res) {
  const { username, usertx} = req.body;
  let create_time = Fun(),
    updata_up = Fun();
  let sql = `insert into user (username,usertx,create_time) values('${username}', '${usertx}', '${create_time}')`;
  let data = await mysql(sql);
  res.json({
    code: 200,
    data,
  });
});
router.get("/getuser", async function (req, res) {
  const { username} = req.query;
  // console.log(username)
  let sql = `SELECT * FROM user where username = '${username}'`;
  let data = await mysql(sql);
  res.json({
    code: 200,
    data,
  });
});
router.post("/changeuser", async function (req, res) {
  const { username,direction} = req.body;
  // console.log(username)
  let sql = `update user set direction='${direction}' where username = '${username}'`
  let data = await mysql(sql);
  res.json({
    code: 200,
    data,
  });
});
exApp.get("/getwork", async function (req, res) {
  let { name } = req.query;
  let sql = `SELECT * FROM lxfxcx WHERE userid LIKE '%${name}%'`;
  let data = await mysql(sql);
  res.json({
    code: 1,
    data,
  });
});
exApp.get("/getlike", async function (req, res) {
  let { name } = req.query;
  let sql = `SELECT * FROM lxfxcx WHERE likes LIKE '%${name}%'`;
  let sqll = `SELECT * FROM home WHERE likes LIKE '%${name}%'`;
  let data = await mysql(sql);
  let list = await mysql(sqll);
  res.json({
    code: 1,
    data,
	list
  });
});
exApp.get("/getcollection", async function (req, res) {
  let { name } = req.query;
  let sqll = `SELECT * FROM home WHERE collection LIKE '%${name}%'`;
  let sql = `SELECT * FROM lxfxcx WHERE collection LIKE '%${name}%'`;
  let data = await mysql(sql);
  let list = await mysql(sqll);
  res.json({
    code: 1,
    data,
	list
  });
});

exApp.use("/", router);
